<?php 
	class Member extends CI_Controller{
		public function __construct(){
			parent::__construct();
		}
		
		public function login(){
			$arrs = array();
			if ( isset($_POST['name']) && isset($_POST['pass']) && isset($_POST['customer_name']) && isset($_POST['imie']) ) {
				//// android
				$username 		=trim($this->input->post("name"));
				$password			=trim($this->input->post("pass"));
				$customer_name = trim($this->input->post("customer_name"));
				$imie					=trim($this->input->post("imie"));
			}else{
				/// ios
				$data = $_POST;

				$username 		=trim($data['username']);
				$password			=trim($data['password']);
				$customer_name = trim($data['customer_name']);
				$imie					=trim($data['imie']);
			}
			if($username != "" && $password !=""){
				$this->db->select("t2.pkid,t2.demo,t2.status,t1.pkid as cid");
				$this->db->where("t2.card_no",$username);
				$this->db->where("t2.password",$password);
				$this->db->where("t1.name",$customer_name);
				$this->db->where("t2.status",1);
				$this->db->from("mobile_customer t1");
				$this->db->join("mobile_card t2", "t1.pkid=t2.customer_pkid");
				$res = $this->db->get()->row();
				if(count($res) > 0){
					if($res->demo == 0){ // is login
						$this->db->where("card_pkid",$res->pkid);
						$num = $this->db->count_all_results("mobile_member");
						if($num == 0){
							$arrs['StatusID']  	="2";
							$arrs['Error']			="";
							$arrs['status']		=$res->pkid;
						}else{
							$this->db->where("imei",$imie);
							$this->db->where("customer_pkid",$res->cid);
							$num2 = $this->db->count_all_results("mobile_member");
							if($num2 == 1){
								$arrs['StatusID']  	= "1";
								$arrs['Error']			="";
								$arrs['status']		="";
							}else{
								$arrs['StatusID']  		= "0";
								$arrs['Error']				="Duplicate Login";
								$arrs['status']			="";
							}
						}
					}else{ // is not login
						$arrs['StatusID']  	= "1";
						$arrs['Error']			="";
						$arrs['status']		="";
					}
				}else{
					$arrs['StatusID']  		= "0";
					$arrs['Error']				=" Invalid Card No. or Password not found";
					$arrs['status']			="";
				}
			}else{
				$arrs['StatusID']  		= "0";
				$arrs['Error']				=" Invalid Card No. or Password not found";
				$arrs['status']			="";
			}
			echo json_encode($arrs);
		}
		
		public function signup(){
			$arrs = array();
			$name 		= $this->input->post('name');
			$email		= $this->input->post('email');
			$mobile		= $this->input->post('phone'); 
			$country    = $this->input->post('country');
			$pkid 		= $this->input->post('pkid');
			$imie 		= $this->input->post('imie');
			//$pkid 		= "1";
			$address	= $this->input->post('address');
			//check partner
			$this->db->select("customer_pkid");
			$this->db->where("pkid",$pkid);
			$row = $this->db->get("mobile_card")->row();
			
			//check country
			$this->db->select("pkid");
			$this->db->where("name",$country);
			$rowc = $this->db->get("mobile_country")->row();
				$data = array(
							"customer_pkid"=>$row->customer_pkid,
							"card_pkid"=>$pkid,
							"email"=>$email,
							"name"=>$name,
							"mobile"=>$mobile,
							"address"=>$address,
							"country"=>$rowc->pkid,
							"imei"=>$imie,
	 						"created_date"=>date("Y-m-d H:i:s")
						);
				$resin = $this->db->insert("mobile_member",$data);			
					if($resin){
						$arrs['StatusID']  	= "1";
						$arrs['Error']			="";
					}else {
						$arrs['StatusID']  	= "0";
						$arrs['Error']			="Register fail";
					}
					echo json_encode($arrs);
		}
		
		public function check_imie(){
			$imie = $this->input->post("imie");
			if(trim($this->input->post("customer_name")) != ""){
				$name	= $this->input->post("customer_name");
				$this->db->select("pkid");
				$this->db->where("name",$name);
				$row = $this->db->get("mobile_customer")->row();
				$this->db->where("customer_pkid",$row->pkid);
				$this->db->where("imei",$imie);
				$num = $this->db->count_all_results("mobile_member");
			}else{
				$this->db->where("imei",$imie);
				$num = $this->db->count_all_results("mobile_member");
			}
			if($num == 0){
				$arrs['StatusID']  	= "1";
				$arrs['Error']			="";
			}else{
				$arrs['StatusID']  	= "0";
				$arrs['Error']			="";
			}
			echo json_encode($arrs);
		}	
		
		public function getfriend($str=""){
			$this->db->where("name",$str);
			$row = $this->db->get("mobile_customer")->row();
			$to			=$row->email;
			try{
				$config = Array(
						'protocol' => 'smtp',
						'smtp_host' => 'ssl://smtp.googlemail.com',
						'smtp_port' => 465,
						'smtp_user' => 'eshinetv@gmail.com',
						'smtp_pass' => '#hotelsolution%',
						'mailtype'  => 'html',
						'charset' => 'utf-8',
						'wordwrap' => TRUE
				);
				$detail = '
				<p><strong>Friend Get Friend</strong></p>
				<p> ชื่อผู้แนะนำ<br/>
				&nbsp;&nbsp;&nbsp;&nbsp;-'.$this->input->post('name').'</p>
				<p>เบอร์โทรศัพท์<br/>
				&nbsp;&nbsp;&nbsp;&nbsp;-'.$this->input->post('name_phone').'</p>
				<p>ชื่อเพื่อน<br/>
				&nbsp;&nbsp;&nbsp;&nbsp;-'.$this->input->post('friend_name').'</p>
				<p>เบอร์โทรเพื่อน<br/>
				&nbsp;&nbsp;&nbsp;&nbsp;-'.$this->input->post('friend_phone').'</p>
				<p>อีเมลล์เพื่อน<br/>
				&nbsp;&nbsp;&nbsp;&nbsp;-'.$this->input->post('email').'</p>';
				$this->load->library('email', $config);
				$this->email->set_newline("\r\n");
				$this->email->from($this->input->post('email')," Friend Get Friend");
				//$this->email->to("sthnews4@gmail.com");
				$this->email->to($to);
				$this->email->subject("Friend Get Friend");
				$this->email->message($detail);
				$this->email->send();
				$this->email->print_debugger();
				$arr['StatusID']  = "1";
				$arr['Error']		="";
			}catch (Exception $e){
				$arr['StatusID']  = "0";
				$arr['Error']		="Send mail error!";
			}
				
			echo json_encode($arr);
		}
		
	}	
?>